<!--
 * @Descripttion: 
 * @version: 
 * @Author: Peanut
 * @Date: 2024-04-20 11:30:02
 * @LastEditors: Peanut
 * @LastEditTime: 2024-04-29 11:13:24
-->
<template>
  <div class="homePage">
    <el-container>
      <el-main>
        <el-card class="accounceBox">
          <i
            class="el-icon-message-solid"
            style="margin-right: 18px; font-size: 20px"
            >公告：</i
          >

          <div class="scrolling-text">
            <span>{{ announce }}</span>
          </div>
        </el-card>
        <h1>我的信息 <el-button @click="amendInfo">点击修改</el-button></h1>

        <el-card style="width: 40%">
          <p>
            <i class="el-icon-s-home" style="margin-right: 18px"></i
            >用户名称：{{ name }}
          </p>
          <p>
            <i class="el-icon-s-custom" style="margin-right: 18px"></i
            >用户类型：{{
              userType == "user"
                ? "用户"
                : userType == "admin"
                ? "管理员"
                : "未知"
            }}
          </p>
          <!-- <p>
            <i class="el-icon-location" style="margin-right: 18px"></i
            >坐标位置：地球
          </p>
          <p>
            <i class="el-icon-s-promotion" style="margin-right: 18px"></i>email:
            66666666
          </p> -->
        </el-card>
      </el-main>
      <el-dialog
        title="修改信息"
        :visible.sync="amendInfoShow"
        width="600px"
        height="400px"
      >
        <div class="amendPage">
          <el-form
            :model="ruleForm"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="修改后用户名" prop="name">
              <el-input
                v-model.number="ruleForm.name"
                placeholder="请输入账号"
                prefix-icon="el-icon-lollipop"
              ></el-input>
            </el-form-item>
            <el-form-item label="修改后的密码" prop="password">
              <el-input
                v-model="ruleForm.password"
                placeholder="请输入密码"
                show-password
                prefix-icon="el-icon-ice-cream-round"
              ></el-input>
            </el-form-item>
          </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" type="primary" @click="handleClose"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </el-container>
  </div>
</template>

<script>
import Announcement from "./announcement.vue";
export default {
  name: "home",
  components: { Announcement },
  data() {
    return {
      name: JSON.parse(sessionStorage.getItem("userData")).name,
      userType: JSON.parse(sessionStorage.getItem("userData")).role,
      amendInfoShow: false,
      ruleForm: {
        name: "",
        password: "",
      },
      announce: "",
      // ggList: [],
    };
  },
  mounted() {
    this.getAccouncement();
  },
  methods: {
    //页面加载时调用公告展示接口
    getAccouncement() {
      axios.get("http://localhost:8080/user/showAnnouncement").then((res) => {
        if (res.code == 1) {
          this.announce = res.data.text;
          // this.ggList.push({ noticeContent: res.data.text });
        }
      });
    },
    //用户点解修改信息按钮会将弹框展示出来
    amendInfo() {
      this.amendInfoShow = true;
    },
    //关闭弹窗的操作，调用修改信息接口
    handleClose() {
      axios
        .post("http://localhost:8080/user/update", {
          ...this.ruleForm,
          id: JSON.parse(sessionStorage.getItem("userData")).id,
        })
        .then((res) => {
          if (res.code == 1) {
            this.amendInfoShow = false;
            this.$message({
              message: "修改成功！！请重新登录",
              type: "success",
            });
            sessionStorage.clear();
            this.$router.push("/");
          }
        });
    },
  },
};
</script>

<style scoped lang="scss">
.homePage {
  .accounceBox {
    /deep/.el-card__body {
      width: 100%;
      display: flex;
      align-items: center;
      i {
        width: 10%;
      }
      .scrolling-text {
        overflow: hidden;
        white-space: nowrap;
        width: 85%;
        /* text-align: center; */
      }

      .scrolling-text span {
        font-size: 20px;
        display: inline-block;
        padding-left: 100%;
        animation: marquee 20s linear infinite; /* 调整动画时长和效果 */
      }

      @keyframes marquee {
        0% {
          transform: translateX(0);
        }
        100% {
          transform: translateX(-100%);
        }
      }
    }
  }

  .el-button {
    margin-left: 20px;
  }
  .amendPage {
    .el-form-item__label {
    }
  }
}
</style>
